﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PayrollHR.Web;
using Payroll.Common.Utils;
using Payroll.BusinessLogic;
using Payroll.BusinessLogic.Directory;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using MigraDoc.DocumentObjectModel;

namespace PayrollHR.Administration.Modules
{
    public partial class CustomerInfoControl : BasePayrollAdministrationUserControl
    {
        public int CustomerID
        {
            get
            {
                return ddlCustType.Enabled ? 0 : CommonHelper.QueryStringInt("CustomerId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { }
        }
        public void FillDropDown()
        {
            FillContriesDropDowns();
            FillNational();
            FillCustTypeDropDowns();
        }
        /// <summary>
        /// Binds the customer.
        /// </summary>
        /// <param name="item">The item.</param>
        public void BindCustomer(Customer item)
        {
            if (CommonHelper.IsNull(item))
                return;
            txtCustName.Text = item.CustName;
            txtCustCaseWork.Text = item.CustCaseWork;
            if (CommonHelper.ToString(item.CustDate) != "")
            {
                dpCustDate.SelectedDate = CommonHelper.ToDate(item.CustDate);
            }
            txtCustHour.Text = item.CustHour;
            dpCustDateOfBirth.Text = item.CustDateOfBirth;
            txtCustPlate.Text = item.CustPlate;
            txtCustParent.Text = item.CustParent;
            txtCustFax.Text = item.CustFax;
            txtCustAddres.Text = item.CustAddres;
            txtCustEmail.Text = item.CustEmail;
            if (item.CustMobile != "")
            {
                txtCustMobile.Text = item.CustMobile;
            }
            txtCustCompany.Text = item.CustCompany;
            if (item.CustPhone != "")
            {
                txtCustPhone.Text = item.CustPhone;
            }
            txtCustEdu.Text = item.CustEdu;
            txtCustFax.Text = item.CustFax;
            txtCustLangue.Text = item.CustLangue;
            txtCustCareer.Text = item.CustCareer;

            cbIsVisa.Checked = item.isVisa;

            if (!CommonHelper.IsNull(item.NationalId))
            {
                ddlNationalId.SelectedValue = item.NationalId.ToString();
            }
            txtVisaNumber.Value = item.VisaNumber;
            txtCustYear.Text = item.CustYear;
            txtCustSource.Text = item.CustSource;
            RdButtonLisStudyTravel.SelectedIndex = item.IsStudy;
            txtstudyTravelOther.Text = item.studyTravelOther;
            if (!CommonHelper.IsNull(item.NationalId))
            {
                ddlCustNationalId.SelectedValue = item.CustNationalId.ToString();
            }
            if (!CommonHelper.IsNull(item.CutomerTypeId))
            {
                ddlCustType.Items.FindByValue(CommonHelper.ToString(item.CutomerTypeId)).Selected = true;
                ddlCustType.Enabled = false;
            }
            else
            {
                ddlCustType.Enabled = true;
            }

            txtCustCity.Text = item.CustCity;
            txtCustState.Text = item.CustState;
            txtCustLevel.Text = item.CustLevel;
            txtTravelNote.Text = item.TravelNote;
            txtCustStady.Text = item.CustStady;
            txtCustSchool.Text = item.CustSchool;
            txtCustRelative.Text = item.CustRelative;
            txtCustRelativeAddre.Text = item.CustRelativeAddre;
            txtCustNote.Text = item.CustNote;
            dpCustFollowUpDate01_01.SelectedDate = CommonHelper.ToDate(item.CustFollowUpDate01.Split(';')[0]);
            txtCustFollowUpDate01_01.Text = item.CustFollowUpDate01.Split(';')[1];

            dpCustFollowUpDate02_01.SelectedDate = CommonHelper.ToDate(item.CustFollowUpDate02.Split(';')[0]);
            txtdpCustFollowUpDate02_01.Text = item.CustFollowUpDate02.Split(';')[1];

            dpCustFollowUpDate03_01.SelectedDate = CommonHelper.ToDate(item.CustFollowUpDate03.Split(';')[0]);
            txtCustFollowUpDate03_01.Text = item.CustFollowUpDate03.Split(';')[1];

            dpCustNewNex01_01.SelectedDate = CommonHelper.ToDate(item.CustNewNex01.Split(';')[0]);
            txtCustNewNex01_01.Text = item.CustNewNex01.Split(';')[1];

            dpCustNewNex02_01.SelectedDate = CommonHelper.ToDate(item.CustNewNex02.Split(';')[0]);
            txtCustNewNex02_01.Text = item.CustNewNex02.Split(';')[1];

            dpCustNewNex03_01.SelectedDate = CommonHelper.ToDate(item.CustNewNex03.Split(';')[0]);
            txtCustNewNex03_01.Text = item.CustNewNex03.Split(';')[1];

            //dpCustIsDate.SelectText = item.CustIsDate;

            if (item.CustSignature)
            {
                cbCustSignature.Checked = true;
            }
            /// new
            /// 
            txtbusiness.Text = item.businesstype;
            txtRealEstate.Text = item.busRealEstateInfo;
            txtTax.Text = item.busRealMonthlyTax;
            txtincome.Text = item.busMontlyIncome ?? "0";
            txtOrther.Text = item.busOther;
            //yeu cau khach hang
            txtrequirements.Text = item.requirements;
            // Giai thich yeu cau visa
            cbLevel.Checked = item.requirements_learning;
            cbAccoutNumber.Checked = item.requirements_accountNumber;
            cbDemonstrateIncome.Checked = item.requirements_income;
            cbConstraintConditions.Checked = item.requirements_conditions;
            cbFinan.Checked = item.requirements_financial;
            cbWorking.Checked = item.requirements_work;
            cbResponsibility.Checked = item.requirements_responsibility;
            txtMarketing.Text = item.marketing;
            txtResults.Text = item.marketing_results;
            /// thanh toan
            txtFirst.Text = item.payment_1;
            txtSecond.Text = item.payment_2;
            txtSTK.Text = item.payment_accountNumber;
            txtTC.Text = item.payment_financial;
            txtAN.Text = item.payment_AN;
            txtGD.Text = item.payment_GD;
            txtPVVISA.Text = item.payment_PV_VISA;
            txtOther1.Text = item.payment_Orther_1;
            txtlblOther2.Text = item.payment_Orther_2;
            txtOther3.Text = item.payment_Orther_3;
            txtSub1.Text = item.payment_Sub_1;
            txtSub2.Text = item.payment_Sub_2;
            txtTotal.Text = item.payment_Total;
            txtPlan.Text = item.plan_plan;
            txtNote.Text = item.Note_note;
            txtSK.Text = item.payment_SK;
            txtOther4.Text = item.payment_Orther_4;
            txtCounselor.Text = item.CustCounselor;
            txtToNation.Text = item.SourceToNation;
            chkPetition.Checked = item.SourcePetition;
            txtCustIsDate.Text = item.CustIsDate;
            this.imgCustomer.ImageUrl = item.imageUrl;
        }
        private void FillCustTypeDropDowns()
        {
            ddlCustType.Items.Clear();
            var customertypeColection = this.CustomerService.GetCustomerType();
            customertypeColection.Insert(0, new CutomerType { CutomerTypeId = 0, CutomerTypeName = "-None-" });
            foreach (var customertype in customertypeColection)
            {
                ListItem ddlCustTypeItem2 = new ListItem(customertype.CutomerTypeName, customertype.CutomerTypeId.ToString());
                ddlCustType.Items.Add(ddlCustTypeItem2);
            }
        }
        private void FillContriesDropDowns()
        {
            ddlCustNationalId.Items.Clear();
            var languageCollection = this.CustomerService.GetObjectById(1);
            if (languageCollection.Count != 1)
            {
                languageCollection.Insert(0, new Objects { ObjectId = 0, ObjectName = "-None-" });
            }
            foreach (Objects language in languageCollection)
            {
                ListItem ddlChannelItem2 = new ListItem(language.ObjectName, language.ObjectId.ToString());
                ddlCustNationalId.Items.Add(ddlChannelItem2);
            }
        }
        public void FillNational()
        {
            ddlNationalId.Items.Clear();
            var languageCollection = this.CustomerService.GetObjectById(1);
            if (languageCollection.Count != 1)
            {
                languageCollection.Insert(0, new Objects { ObjectId = 0, ObjectName = "-None-" });
            }
            for (int i = 0; i < languageCollection.Count; i++)
            {
                var ddlChannelItem2 = new ListItem(languageCollection[i].ObjectName, languageCollection[i].ObjectId.ToString());
                ddlNationalId.Items.Add(((ListItem)ddlChannelItem2));
            }
        }
        public Customer GetCustomer()
        {
            int? NationalId;
            if (ddlNationalId.SelectedIndex > 0)
                NationalId = CommonHelper.ToInteger(ddlNationalId.SelectedItem.Value);
            else
                NationalId = null;
            int? CustNationalId;
            if (ddlCustNationalId.SelectedIndex > 0)
                CustNationalId = CommonHelper.ToInteger(ddlCustNationalId.SelectedItem.Value);
            else
                CustNationalId = null;

            int UserId = PayrollContext.Current.Session.EmployeeId;

            Customer customer = new Customer
            {
                CustomerID = this.CustomerID > 0? this.CustomerID : 0,
                CustName = txtCustName.Text.Trim(),
                CustCaseWork = txtCustCaseWork.Text,
                CutomerTypeId = int.Parse(ddlCustType.SelectedItem.Value),
                CustDate = dpCustDate.SelectText,
                CustHour = txtCustHour.Text,
                CustDateOfBirth = dpCustDateOfBirth.Text,
                CustPlate = txtCustPlate.Text,
                CustParent = txtCustParent.Text,
                CustEmail = txtCustEmail.Text,
                CustAddres = txtCustAddres.Text,
                CustCompany = txtCustCompany.Text,
                CustMobile = txtCustMobile.Text.Trim(),
                CustPhone = txtCustPhone.Text,
                CustEdu = txtCustEdu.Text,
                CustFax = txtCustFax.Text,
                CustLangue = txtCustLangue.Text,
                CustCareer = txtCustCareer.Text,
                isVisa = cbIsVisa.Checked ? true : false,
                NationalId = NationalId,
                CustYear = txtCustYear.Text,
                VisaNumber = CommonHelper.ToInteger(txtVisaNumber.Value), /*note*/
                isTravel = true,
                TravelNation = "", /*note*/
                TravelNote = txtTravelNote.Text,
                CustSource = txtCustSource.Text,
                IsStudy = RdButtonLisStudyTravel.SelectedIndex,
                CustNationalId = CustNationalId,
                CustCity = txtCustCity.Text,
                CustState = txtCustState.Text,
                CustLevel = txtCustLevel.Text,
                CustStady = txtCustStady.Text,
                CustSchool = txtCustSchool.Text,
                CustRelative = txtCustRelative.Text,
                CustRelativeAddre = txtCustRelativeAddre.Text,
                CustNote = txtCustNote.Text,
                CustFollowUpDate01 = String.Format("{0};{1}", dpCustFollowUpDate01_01.SelectText, txtCustFollowUpDate01_01.Text.Trim()),
                CustFollowUpDate02 = String.Format("{0};{1}", dpCustFollowUpDate02_01.SelectText, txtdpCustFollowUpDate02_01.Text.Trim()),
                CustFollowUpDate03 = String.Format("{0};{1}", dpCustFollowUpDate03_01.SelectText, txtCustFollowUpDate03_01.Text),
                CustNewNex01 = String.Format("{0};{1}", dpCustNewNex01_01.SelectText, txtCustNewNex01_01.Text),
                CustNewNex02 = String.Format("{0};{1}", dpCustNewNex02_01.SelectText, txtCustNewNex02_01.Text),
                CustNewNex03 = String.Format("{0};{1}", dpCustNewNex03_01.SelectText, txtCustNewNex03_01.Text),
                CustIsDate = txtCustIsDate.Text.Trim(),
                CustSignature = cbCustSignature.Checked,
                studyTravelOther = txtstudyTravelOther.Text, // "1", /*ProDate = item.ProDate,*/ /*ProCurator = item.ProCurator,*/ /*ProFullName = item.ProFullName,*/ /*ProSource = item.ProSource,*/ /*ProSchool = item.ProSchool,*/ /*ProProgram = item.ProProgram,*/ /*ProDateOfAdmission = item.ProDateOfAdmission,*/ /*ProscheduledInterviewDate = item.ProscheduledInterviewDate,*/ /*ProOther = item.ProOther,*/ /*ProBaccalaureate = item.ProBaccalaureate,*/ /*ProGraduateIndustry = item.ProGraduateIndustry,*/ /*ProGraduateYear = item.ProGraduateYear,*/ /*ProWorking = item.ProWorking,*/ /*ProLearning = item.ProLearning,*/
                Deleted = false,
                UpdateByID = UserId,
                CreateByID = UserId,
                CreatedOn = DateTime.Now,
                UpdatedOn = DateTime.Now,
                businesstype = txtbusiness.Text,
                busRealEstateInfo = txtRealEstate.Text,
                busRealMonthlyTax = txtTax.Text.Trim(),
                busMontlyIncome = txtincome.Text,
                busOther = txtOrther.Text,
                //yeu cau khach hang
                requirements = txtrequirements.Text,
                // Giai thich yeu cau visa
                requirements_learning = cbLevel.Checked,
                requirements_accountNumber = cbAccoutNumber.Checked,
                requirements_income = cbDemonstrateIncome.Checked,
                requirements_conditions = cbConstraintConditions.Checked,
                requirements_financial = cbFinan.Checked,
                requirements_work = cbWorking.Checked,
                requirements_responsibility = cbResponsibility.Checked,
                marketing = txtMarketing.Text,
                marketing_results = txtResults.Text,
                /// thanh toan
                payment_1 = txtFirst.Text,
                payment_2 = txtSecond.Text,
                payment_accountNumber = txtSTK.Text,
                payment_financial = txtTC.Text,
                payment_AN = txtAN.Text,
                payment_GD = txtGD.Text,
                payment_PV_VISA = txtPVVISA.Text,
                payment_Orther_1 = txtOther1.Text,
                payment_Orther_2 = txtlblOther2.Text,
                payment_Orther_3 = txtOther3.Text.Trim(),
                payment_Sub_1 = txtSub1.Text.Trim(),
                payment_Sub_2 = txtSub2.Text.Trim(),
                payment_Total = txtTotal.Text.Trim(),
                plan_plan = txtPlan.Text.Trim(),
                Note_note = txtNote.Text.Trim(),
                CustCounselor = txtCounselor.Text.Trim(),
                payment_SK = txtSK.Text.Trim(),
                payment_Orther_4 = txtOther4.Text.Trim(),
                SourcePetition = chkPetition.Checked,
                SourceToNation = txtToNation.Text.Trim(),
                PictureBinary = fuProductPicture1.HasFile ? fuProductPicture1.FileBytes : new byte[0],
                MimeType = fuProductPicture1.HasFile ? fuProductPicture1.PostedFile.ContentType : "",
                imageUrl = Server.MapPath("~/images/Customer/")
            };
            return customer;
        }

        protected void ddlCustType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int type = int.Parse(ddlCustType.SelectedItem.Value);
            txtCustCaseWork.Text = this.CustomerService.GetCaseWork(type);
        }

        protected void btnDuplicate_Click(object sender, EventArgs e)
        {
            ddlCustType.Enabled = true;
        }
    }
}